package com.songlanyun.modules.ad.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.modules.ad.service.AdService;
import com.songlanyun.modules.exception.AdException;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.ad.dao.AdLocationDao;
import com.songlanyun.modules.ad.entity.AdLocationEntity;
import com.songlanyun.modules.ad.service.AdLocationService;

import javax.annotation.Resource;


@Service("yxtAdLocationService")
public class AdLocationServiceImpl extends ServiceImpl<AdLocationDao, AdLocationEntity> implements AdLocationService {

    @Resource
    private AdService adService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = MapUtil.getStr(params, "name");
        Integer adLocationType = MapUtil.getInt(params, "adLocation_type");//0广告位的类型（普通广告位，一次只能随机显示一张广告图），1、幻灯广告位(按顺序显示
        Integer ownerType = MapUtil.getInt(params, "owner_type");//广告位类型（0，平台广告位，1商家店铺广告位）

        IPage<AdLocationEntity> page = this.page(
                new Query<AdLocationEntity>().getPage(params),
                new LambdaQueryWrapper<AdLocationEntity>()
                        .eq(adLocationType != null, AdLocationEntity::getAdLocationType, adLocationType)
                        .eq(ownerType != null, AdLocationEntity::getOwnerType, ownerType)
                        .like(StringUtils.isNotBlank(name), AdLocationEntity::getName, name)
        );

        return new PageUtils(page);
    }

    @Override
    public List<AdLocationEntity> select(Map<String, Object> params) {
        Integer ownerType = MapUtil.getInt(params, "owner_type");

        List<AdLocationEntity> list = this.list(
                new LambdaQueryWrapper<AdLocationEntity>()
                        .eq(ownerType != null, AdLocationEntity::getOwnerType, ownerType)
        );


        return list;
    }

    @Override
    public AdLocationEntity getByMarkCode(String markCode, boolean isThrow) {
        AdLocationEntity adLocationEntity = this.getOne(
                new LambdaQueryWrapper<AdLocationEntity>()
                        .eq(AdLocationEntity::getMarkCode, markCode)
        );

        if (adLocationEntity == null) {
            if (isThrow) {
                throw new RRException(AdException.AD_LOCATION_NOT_EXIST);
            }
        }
        return adLocationEntity;
    }

    @Override
    public void delById(Integer id) {
        //该广告位下已创建广告的，不能被删除
        AdLocationEntity adL = this.baseMapper.selectById(id);
        if (adL != null) {
            int count = adService.countByMarkCode(adL.getMarkCode());
            if (count > 0) {
                throw new RRException(AdException.AD_LOCATION_NOT_DEL);
            }
            //删除
            this.baseMapper.deleteById(id);
        }

    }

}
